#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <cstring>
#include <thread>
#include <vector>
#include <errno.h>
#include <cstring>

using namespace std;
int ticket = 1000; //票数量
void *buyticket(void *num)
{
    int n = (long)num;
    while (true)
    {
        if (ticket > 0)
        {
            usleep(1000);
            ticket--;
            printf("%d 线程买到票,剩余 %d 张\n", n, ticket);
        }
        else
        {
            break;
        }
    }
}

int main()
{
    vector<pthread_t> v;
    for (int i = 0; i < 3; i++)
    {
        pthread_t tid;
        pthread_create(&tid, nullptr, buyticket, (void*)(long)i);
        v.push_back(tid);
    }

    for(auto ch : v)
    {
        pthread_join(ch, nullptr);
    }

    return 0;
}